package com.daka.tools.rongYun;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.daka.tools.rongYun.models.SdkHttpResult;
import com.daka.tools.rongYun.util.HttpUtil;

import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright: Copyright (c)2016
 * Company: 志愿者打卡器(www.dakaqi.cn)
 * 类名称: RongCloudSMS <br>
 * 类描述: <br>
 *
 * @author: ChunYang.Zhang
 * @since: 2016/7/20 14:50
 * @version: 1.0.0
 */
public class RongCloudSMS
{
    private static final String appKey = PushMessageUtil.KEY;
    private static final String appSecret = PushMessageUtil.SECRET;
    private static final String UTF8 = "UTF-8";

//    模版 ID 	名称 	内容 	模版类型
//    3Hymah4_4CdaHMEUyH3RiF 	用户注册 	【上海悠活】您的短信验证码是{验证码}... 	短信验证
//    5yWBzF04kD0befUFWdvQ5k 	密码找回 	【上海悠活】您的密码找回验证码是{验证... 	短信验证
//    6rD9XkMcAjm8_XsdhgbZ_Z 	修改密码 	【上海悠活】您的修改密码验证码是{验证... 	短信验证
//    fg-G0gos4m89dmIotkPQ6L 	身份验证 	【上海悠活】您的身份验证的短信验证码是... 	短信验证
//    dAiStOfWAuoal6zDIcd5ZH 	通用验证码 	【上海悠活】您的短信验证码是{验证码}... 	短信验证


    /**
     * 发送短信验证码 方法
     * @param mobile
     * @throws Exception
     */
    public static String sendCode(String mobile)throws Exception
    {
        String sessionId = "";
        String url = "http://api.sms.ronghub.com/sendCode.json";
        String templateId = "dAiStOfWAuoal6zDIcd5ZH";
        Map<String,String> param = new HashMap<String, String>();
        param.put("mobile",mobile);
        param.put("templateId",templateId);
        param.put("region","86");

        SdkHttpResult result = post(url, param);
        if(null != result && result.getHttpCode() == 200)
        {
            JSONObject jsonObject = JSON.parseObject(result.getResult());
            sessionId = jsonObject.getString("sessionId");
        }
        return sessionId;
    }

    /**
     * 验证码验证
     * @param sessionId
     * @param code
     * @return
     * @throws Exception
     */
    public static boolean verifyCode(String sessionId,String code)throws Exception
    {
        System.out.println(sessionId + "---------------->" + code);
        String url = "http://api.sms.ronghub.com/verifyCode.json";
        Map<String,String> param = new HashMap<String, String>();
        param.put("sessionId",sessionId);
        param.put("code",code);


        SdkHttpResult result = post(url, param);
        if(null != result && result.getHttpCode() == 200)
        {
            return true;
        }
        return false;
    }
    private static SdkHttpResult post(String url,Map<String,String> param)
            throws Exception
    {

        HttpURLConnection conn = HttpUtil.CreatePostHttpConnection(appKey, appSecret, url);

        StringBuilder sb = new StringBuilder();
        if (param != null)
        {
            int count = 0;
            for (Map.Entry<String, String> entry : param.entrySet())
            {
                //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                if(0== count)
                {
                    sb.append(entry.getKey()+"=").append(URLEncoder.encode(entry.getValue(), UTF8));
                }
                else
                {
                    sb.append("&"+entry.getKey()+"=").append(URLEncoder.encode(entry.getValue(), UTF8));
                }
                ++count;
            }
        }
        //System.out.println(sb.toString());
        HttpUtil.setBodyParameter(sb, conn);
        SdkHttpResult result = HttpUtil.returnResult(conn);
        System.out.println(result);
        return result;
    }

    public static void main(String[] args)
    {
        try
        {
            //String sessionId = sendCode("15821117931");
            //System.out.println("sessionId------------->"+sessionId);

            String sessionId = "5VzV94WZAo2agxPaFNj2xI";
            verifyCode(sessionId,"788126");
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
